<?php
/*
Template Name: Guestbook
*/
?>
<?php
function addHeaderCode() {
    echo '<link type="text/css" rel="stylesheet" href="'.get_bloginfo('wpurl').'/wp-content/plugins/wp-livechat/style/ajaxchat.css"/>'."\n";
}
add_action('wp_head','addHeaderCode');
wp_enqueue_script('jquery',
get_bloginfo('wpurl').'/wp-includes/js/jquery/jquery.js');
// import the ajax js
//wp_enqueue_script('newscript',get_bloginfo('wpurl').'/wp-content/plugins/wp-livechat/js/jq_ajax_chat.js.php');
// import jquery libary
// Get room list
$sql="select * from `wp_chatroom` ";
$wp_rooms=$wpdb->get_results($sql);

// Get the user list
$sql="Select * from `wp_users` order by `display_name`";
$wp_users=$wpdb->get_results($sql);

$current_info=wp_get_current_user();

$current_id=$current_info->id;

$login_url=get_bloginfo('wpurl').'/wp-login.php';
if($current_id=='')
{   get_header();
    echo "<p class='login'>You are not logged in. Please login <a href='".get_bloginfo('wpurl')."/reception/profileregistration/'>here</a></p>";
    exit;
    get_footer();
}
wp_enqueue_script('livechat',
get_bloginfo('wpurl').'/wp-content/plugins/wp-livechat/js/ajaxchat.js.php');

// Retrieve last message!!
$options = get_option("live_chat");
$sql="Select * from `wp_chat` where `receiver_id`=0 order by `timestamp` desc limit 0,".$options['posts'];
$recent_message=$wpdb->get_results($sql);
$page_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status='publish' AND post_name='live-chat'");
$perma = get_settings('permalink_structure');

$reply_id=isset($_GET['user_id'])?(int)$_GET['user_id']:-1;
$reply_id=user_exists($reply_id)?$reply_id:-1;
function user_exists($id)
{
	global $wpdb;

	if(is_numeric($id))
	{
		$bool = $wpdb->get_var("SELECT ID
		FROM $wpdb->users
		WHERE ID = ANY
		(SELECT user_id FROM $wpdb->usermeta
		WHERE user_id = $id)
		");
	}
	
	if(isset($bool))
		return TRUE;
	else
		return FALSE;
}
$view_tab=isset($_GET['view_tab'])?(int)$_GET['view_tab']:0;

?>
<?php get_header(); ?>
	<div id="sidebar">
	  <form method="post" action="/trunk/index.php">
		  <input type="text" class="keyword" name="s"/>
		  <input type="image" alt="Search" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/search.gif" title="Search" class="submit" name="submit"/>
	 </form>
	
		<div id="side_top">
		<img alt="" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/private.gif" width="107" height="144" />
		<img alt="" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/privat_pic.gif" width="234" height="131" /></div>
		<div id="sidebar_Messages">
			<h3>Recent Chat Messages</h3>
			<?php if(count($recent_message)!=0): ?>
			<ul>
					<?php
						if (!$perma) {
							$page_url = get_bloginfo('wpurl')."/?page_id=$page_id";
						}
						else {
							$page_url = get_bloginfo('wpurl')."/live-chat/";
						}
					?>
					<?php foreach($recent_message as $rm):
						$message_day = mktime(0,0,0,substr($rm->timestamp,5,2),substr($rm->timestamp,8,2),substr($rm->timestamp,0,4));
					?>
						<li>
						<a href="<?php echo $page_url; ?>#m_<?php echo $rm->id ?>" onclick="retrieve_message(this); return false" rel="<?php echo $message_day; ?>"><?php echo substr($rm->text,0,50) ?>
						<?php if(strlen($rm->text) >=50) { echo "..."; } ?>
						</a>
						</li>
	
					<?php endforeach;?>
			</ul>
			<?php endif;?>
		</div>
		  <ul id="list">
			<?php foreach($wp_rooms as $room): ?>
				<li style="background-color:#<?php echo $room->color; ?>">
					<input type="checkbox" id="room_<?php echo $room->chat_room_id; ?>" alt="<?php echo $room->chat_room_id; ?>" name="room" onclick="hideMess(this);" checked="checked" />
					<div class="info2">
						<label for="room_<?php echo $room->chat_room_id; ?>"><?php echo $room->title ?></label>
						<span>Start on <?php echo date('F j, Y g:i A',mktime($room->timestamp)) ?></span>
					</div><br class="clear" />
				</li>
			<?php endforeach; ?>
		</ul>
		<div id="m_calendar">
		<?php
			include_once dirname(dirname(dirname(__FILE__))).'/plugins/wp-livechat/calendar.php';
		?>
		</div>
		<div id="hid">
		<input type="hidden" value="2" name="r_page" id="r_page"/>
		<input type="hidden" value="<?php echo mktime(0,0,0,date("m"),date("d"),date("Y"));?>" name="r_day" id="r_day"/>
		<input type="hidden" value="<?php echo $reply_id ?>" id="reply_id" />
		</div>
	</div>
	<div id="content">
		<div id="msg"></div>
    	<div id="tab">
            	<div class="tab1"></div>
                <div id="tag1" class="tag-selected" onclick="tab('1');"><img alt="" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/collegeChat_a.gif" width="81" height="16" /></div>
                <div class="tab2"></div>
                <div id="tag2" onclick="tab('2');"><img alt="" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/privatechat.gif" width="78" height="16" /></div>
                <div class="tab3"></div>
				<input type="hidden" value="<?php echo $view_tab ?>" id="view_tab" />
		</div>
        <div id="box1">
            <form id="sender_form" action="">
        	<div class="title">
           	  <h2><img alt="" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/label.gif" width="166" height="18" /></h2>
                <select id="chat_room_id" name="chat_room_id" size="1">
                <option value=''>Please choose a Session</option>
                    <?php foreach($wp_rooms as $room): ?>
                <option value="<?php echo $room->chat_room_id ?>"><?php echo $room->title ?></option>
                    <?php endforeach; ?>
                </select>
                <label for="chat_room_id">Choose Session:</label>
            </div>
              <textarea id="message" name="message" cols="" rows=""></textarea>
              <input id="anonymously" name="anonymously" type="checkbox" value="1" /><label for="anonymously">Send this message anonymously</label><span id="alert_info"></span>
              <input name="session_btn" type="button" value="" onclick="jq_insert_message(); return false" />
            </form>

			<br class="clear" />
            <div class="content">
            	<ul id="messageContent">
                   <li style="background-color:#fff;">
                   </li>
                </ul>
				<br class="clear" />
				<div id="scroll-loader" style="display:none;text-align:center;width:100%"><img alt="" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/scroll-ajax-loader.gif" /><br />Loading the next set of posts...</div>
            </div>
        </div>

        <div id="box2" style="display:none;">
        <form id="private_form" action="">
        <div class="title">
           	  <h2><img alt="" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/speakto.gif" width="88" height="18" /></h2>
                <select id="receiver_id" name="receiver_id" size="1">
                    <option value=''>Please choose a Participiant</option>
                    <?php
                    foreach($wp_users as $user)
                    {
                        if($user->ID!=$current_id)
                        {
                            echo '<option value="'.$user->ID.'">'.$user->display_name.'</option>';
                        }
                    }
                    ?>
              </select>
              <label  for="private_message">Choose Participiant:</label>
            </div>
            	<textarea id="private_message" name="private_message" cols="" rows=""></textarea>
                <span id="user_sel_info"></span>
           	  <input name="but2" type="button" value="" onclick="private_ajax_chat(); return false" />
            </form>

			<br class="clear"/>
            <div class="content">
            	<ul id="private_messageContent">
                	<li style="background-color:#fff;">
                    </li>
                </ul>
				<br class="clear" />
				<div id="p-scroll-loader" style="display:none;text-align:center;width:100%"><img alt="" src="<?php echo get_bloginfo('wpurl') ?>/wp-content/plugins/wp-livechat/style/img/scroll-ajax-loader.gif" /><br />Loading the next set of posts...</div>
            </div>
        </div>
    </div>
  	<div id=tip style='position:absolute; width:180px;line-height:14pt;  z-index:1;  background-color: #7D84B9; border: 1px solid gray; overflow: visible;visibility: hidden;font-size:7pt;padding:0;color:#ffffff;font-family:\'Lucida Grande\',Verdana,Arial,Sans-Serif'></div>
  <!--<div id="u_info" style="position:absolute;width:220px;  z-index:1;  background-color: #7D84B9; border: 1px solid gray;height:auto;visibility: hidden;font-size:8pt;padding:12px;color:#ffffff;font-family:'Lucida Grande',Verdana,Arial,Sans-Serif;"></div>-->
  <br class="clear" />
  <script>
var a=jQuery('#sidebar').height();
if (window.XMLHttpRequest) {
 document.getElementById("box1").style.minHeight=a-60+'px';
 document.getElementById("box2").style.minHeight=a-60+'px';
} else {
 document.getElementById("box1").style.height=a-60+'px';
 document.getElementById("box2").style.height=a-60+'px';
}
  </script>
<?php get_footer(); ?>